package org.alex.samples.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.alex.samples.dao.IUserDAO;
import org.alex.samples.model.ExamUserRole;
import org.alex.samples.model.ExamUserVO;
import org.alex.samples.model.ExcelUtils;
import org.alex.samples.model.User;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements IUserService {
	
	@Autowired
	private IUserDAO userDAO;

//	@Override
//	public List<User> findAllUsers() {
//		return userDAO.findAllUsers();
//	}
	
//	@Override
//	public void createUser(User user) throws Exception {
//		userDAO.createUser(user);
//	}
	
	@Override
	public User getUserById(String id) {
		return userDAO.getUserById(id);
	}
	
	@Override
	public void createUser(User user) throws Exception {
		 userDAO.createUser(user);
		 
	}
	
	@Override
	public void editUser(User user) throws Exception {
		 userDAO.editUser(user);
	}

	@Override
	public void delete(String id) throws Exception {
		 userDAO.delete(id);
		
	}
//	@Override
//	public void editUser(User user) throws Exception {
//		userDAO.editUser(user);
//	}
//
//	@Override
//	public void delete(Integer id) throws Exception {
//		userDAO.delete(id);
//		
//	}

	@Override
	public List<ExamUserVO> findAllUsers() {
		List<User> entities = userDAO.findAllUsers();
		if(entities != null){
			ExamUserVO vo = null;
			List<ExamUserVO> resultList = new ArrayList<ExamUserVO>();
			for(User entity : entities){
				vo = new ExamUserVO();
				BeanUtils.copyProperties(entity, vo);
				//Sub subject = subjectDAO.getSubById(entity.getSubject().getId());
				if(entity.getGender() == 0){
					vo.setGender("Male");
				}else if(entity.getGender() == 1){
					vo.setGender("Female");
				}
				resultList.add(vo);
			}
			return resultList;
		}
		return null;
	}

	@Override
	public void saveStudentByExcel(InputStream inputStream, String postfix) {
		Workbook wk = ExcelUtils.openWorkbook(inputStream,postfix);
		List<User> allExcelUser = ExcelUtils.getStudentListByExcel(wk);
		List<User> entities = new ArrayList<User>();
		if(allExcelUser !=null){
			//ExamRow role = examRowDAO.get(25);
			User entity = null;
			for(int i = 0;i< allExcelUser.size();i++){
				entity = new User();
				BeanUtils.copyProperties(allExcelUser.get(i), entity);
				//Set<ExamRole> examRoles = new HashSet<ExamRole>();
				//examRoles.add(role);
				//entity.setExamRoles(examRoles);
				entities.add(entity);
			}
		}
		userDAO.save(entities);
		
		
		
	}

	@Override
	public void updateUserLoginFailCount(User resultVO) {
		userDAO.update(resultVO);
		
	}

	@Override
	public ExamUserRole getExamUserRoleByUserId(String userId) {
		return userDAO.getExamUserRoleByUserId(userId);
	}


	
	
	

}
